package com.xzzz.irda.guardcat.core.metric.rocketmq;

import cn.hutool.core.collection.ConcurrentHashSet;
import cn.hutool.core.util.StrUtil;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Set;

/**
 *
 *
 * @author xzzz
 */
public class RocketMQMetricManager {

    private static final Set<String> PRODUCER_TOPICS = new ConcurrentHashSet<>();
    private static final Set<String> CONSUMER_TOPICS = new ConcurrentHashSet<>();

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    /**
     * 添加 TopicName
     * @param topicName 生产消息的topic
     */
    public void addProducerTopicName(String topicName) {
//        System.out.println("发送的 TOPIC:" + topicName);
        PRODUCER_TOPICS.add(topicName);
    }

    /**
     * 添加 TopicName
     * @param topicName 消费消息的topic
     */
    public void addConsumerTopicName(String topicName) {
//        System.out.println("接收的 TOPIC:" + topicName);
        CONSUMER_TOPICS.add(topicName);
    }


    /**
     * 从 rocketMQTemplate 中获取 topic 集合
     *
     * @return topic 集合
     */
    public Set<String> getProducerTopics() {
        Set<String> topics = rocketMQTemplate.getProducer().getDefaultMQProducerImpl().getPublishTopicList();
        topics.forEach(topic -> {
            if (StrUtil.isNotBlank(topic) && StrUtil.startWith(topic, "topic_")) {
                this.addProducerTopicName(topic);
            }
        });
        return PRODUCER_TOPICS;
    }

    public Set<String> getConsumerTopics() {
        return CONSUMER_TOPICS;
    }
}
